package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.*;


/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

	@Autowired
	private JedisPool jedisPool;

	@Reference
	private SetmealService setMealService;

	/**
	 * 图片上传
	 *
	 * @return
	 */
	@RequestMapping("/upLoad")
	public Result upLoad(@RequestParam("imgFile") MultipartFile imgFile) {
		//获取磁盘里上传的原始文件名
		String originalFilename = imgFile.getOriginalFilename();
		int index = originalFilename.lastIndexOf(".");
		//获得文件后缀名
		String suffix = originalFilename.substring(index - 1);
		//生成一个随机的文件名+后缀名
		String fileName = UUID.randomUUID().toString() + suffix;

		try {
			//调用上传方法，传入要上传的磁盘的文件的对象和随机生成的组成的文件名
			QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
			// [1] 文件上传成功后，将文件名存入一个redis集合中
			jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);

			return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
		} catch (IOException e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
		}
	}

	/**
	 * 添加套餐
	 *
	 * @param setmeal
	 * @param checkgroupIds
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
		try {
			setMealService.add(setmeal, checkgroupIds);
			return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
		}
	}

	/**
	 * 分页查询
	 *
	 * @param queryPageBean
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult add(@RequestBody QueryPageBean queryPageBean) {

		PageResult pageResult = setMealService.findPage(queryPageBean);

		return new PageResult(pageResult.getTotal(), pageResult.getRows());
	}

	//编辑
	@RequestMapping("/findInfoById")
	public Result findInfoById(Integer id) {
		System.out.println(id);
		//根据id查询套餐的所有信息
		Setmeal setmeal = setMealService.findById(id);
		//获取套餐包含的所有检查组的id
		List<Integer> list = new ArrayList<>();

		List<CheckGroup> checkGroups = setmeal.getCheckGroups();
		for (CheckGroup checkGroup : checkGroups) {
			Integer checkGroupId = checkGroup.getId();
			list.add(checkGroupId);
		}
		Map map = new HashMap<>();
		map.put("setmeal", setmeal);
		map.put("checkgroupIds", list);
		return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, map);
	}

	@RequestMapping("/update")
	public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {

		Result result = setMealService.update(setmeal, checkgroupIds);
		return result;

	}

	@RequestMapping("/delete")
	public Result delete(Integer id) {

		try {
			setMealService.delete(id);
			return new Result(true,"删除套餐成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"删除套餐失败");
		}

	}


}
